package com.shizhanzhe.qdd.home.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.home.adapter.ApprenticeAdapter;
import com.shizhanzhe.qdd.home.bean.YxBean;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author YJ
 * @date 2019/1/4
 */
public class ApprenticeDetailsActivity extends Activity {
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.tv_effective)
    TextView tvEffective;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.no_data)
    ImageView noData;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    private ApprenticeAdapter mAdapter;
    private int type = 4;
    private int page = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apprentice_details);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.them)
                .init();
        topTitle.setText("有效徒弟");
        type = getIntent().getIntExtra("type", 4);
        tvTotal.setText(Html.fromHtml("总共收徒<br/><font color='#fe4646'><b>" + getIntent().getIntExtra("tdzs", 0) + "</b></font>人"));
        tvEffective.setText(Html.fromHtml("有效徒弟<br/><font color='#fe4646'><b>" + getIntent().getIntExtra("yxtd", 0) + "</b></font>人"));
        initData();
        setListener();
    }

    private void initData() {
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ApprenticeAdapter(this);
        recycler.setAdapter(mAdapter);
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        if (type==4){
            srl.setEnableRefresh(false);
            srl.setEnableLoadMore(false);
            taskList(page);
        } else {
            topTitle.setText("收徒统计");
//            tablayout.setVisibility(View.VISIBLE);
//            tablayout.addTab(tablayout.newTab().setText("徒弟"), 0, true);
//            tablayout.addTab(tablayout.newTab().setText("徒孙"), 1, false);
//            tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//                @Override
//                public void onTabSelected(TabLayout.Tab tab) {
//                    switch (tab.getPosition()){
//                        case 0:
//                            type=5;
//                            page=1;
//                            taskList(page);
//                            break;
//                        case 1:
//                            type=6;
//                            page=1;
//                            taskList(page);
//                            break;
//                            default:
//                    }
//                }
//
//                @Override
//                public void onTabUnselected(TabLayout.Tab tab) {
//
//                }
//
//                @Override
//                public void onTabReselected(TabLayout.Tab tab) {
//
//                }
//            });
            taskList(page);
        }
    }

    private void setListener() {
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                srl.finishLoadMore(2000);
                taskList(page);
            }
        });
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                srl.finishRefresh(2000);
                taskList(page);
            }
        });
    }

    private void taskList(int cPage) {
        OkHttpDownloadJsonUtil.downloadJson(ApprenticeDetailsActivity.this, Path.INCOMEDETAILS(UserBean.uid, type, cPage), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    try {
                        if (type==4){
                            YxBean.DataBeanX tiBean = gson.fromJson(json, YxBean.DataBeanX.class);
                            if (tiBean.getData().size() > 0) {
                                mAdapter.updateData(page, tiBean.getData());
                            } else {
                                if (page == 1) {
                                    srl.setVisibility(View.GONE);
                                    noData.setVisibility(View.VISIBLE);
                                } else {
                                    Toast.makeText(ApprenticeDetailsActivity.this, "不能再有更多了", Toast.LENGTH_SHORT).show();
                                }
                            }
                        } else {
                            YxBean tiBean = gson.fromJson(json, YxBean.class);
                            Log.d("_________","456");
                            if (tiBean.getData().getData().size() > 0) {
                                mAdapter.updateData(page, tiBean.getData().getData());
                            } else {
                                if (page == 1) {
                                    srl.setVisibility(View.GONE);
                                    noData.setVisibility(View.VISIBLE);
                                } else {
                                    Toast.makeText(ApprenticeDetailsActivity.this, "不能再有更多了", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        //Toast.makeText(ApprenticeDetailsActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(ApprenticeDetailsActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    @OnClick(R.id.top_back)
    public void onViewClicked() {
        finish();
    }
}
